<?php

class UploadComponent {

    function upload_file($file) {

        $data = array();
        $data['Attachments']['name'] = null;
        $data['Attachments']['size'] = null;
        $data['Attachments']['width'] = null;
        $data['Attachments']['height'] = null;
        $data['Attachments']['mime'] = null;
        $data['Attachments']['extra'] = null;
        $data['Attachments']['upload_time'] = null;

        if (!is_array($file) || empty($file['name']))
            return false;
        $path = ARLICLE_WWW . 'attach/' . date('Y/n/j/');
        al_mkdir($path);

        $data['Attachments']['extra'] = strtolower($this->get_extra($file['name']));
        $data['Attachments']['upload_time'] = $this->check_filename($path, time(), $data['Attachments']['extra']);
        $this->file_path = $path . $data['Attachments']['upload_time'] . '.' . $data['Attachments']['extra'];
        if (!move_uploaded_file($file['tmp_name'], $this->file_path ))
            return false;
        if ($image = getimagesize($this->file_path )) {
            $data['Attachments']['width'] = $image[0];
            $data['Attachments']['height'] = $image[1];
            $data['Attachments']['mime'] = $image['mime'];
        }
        unset($file['tmp_name']);
        if ($image) {
            $data['Attachments']['name'] = $file['name'];
            $data['Attachments']['size'] = $file['size'];
            if (get_option('is_watermark')) {
                $this->__watermark();
            }
            $this->file = $data['Attachments'];
            if (get_option('is_thumbnail')) {
                $this->__thumbnail();
            }
        }
        return $data;
    }

    function get_extra($file) {
        $pos = strrpos($file, '.');
        if ($pos !== false)
            return substr($file, $pos + 1);
        else
            return null;
    }

    function check_filename($path, $time, $extra = null) {
        if (file_exists($path . $time . '.' . $extra))
                $time = $this->check_filename($path, $time + 1, $extra);
        return $time;    
    }

    function __watermark() {
        $image = new Image();
        if (!$image->action) {
            return false;
        }
        $image->open($this->file_path);
        $image->watermark(ARLICLE_WWW . 'watermark.gif', wartermark_position(get_option('watermark_position')), get_option('watermark_transparent'));
    }

    function __thumbnail() {
        $image = new Image();
        if (!$image->action) {
            return false;
        }

        $image->open($this->file_path);
        $width = get_option('thumbnail_width');
        $height = get_option('thumbnail_height');
        $thunbnail_sizes = array(0 => array('width ' => $width, 'height' => $height));
        $image_count = 0;
        foreach($thunbnail_sizes as $key => $size) {
            $image_count = $key + 1;
            $image->thumbnail($size);
            $path = str_replace('.', "_$image_count.", $this->file_path);
            $image->save($path);
        }
    }

}

function wartermark_position($position) {
    $results = array(
        'top_left' => 1,
        'top_center' => 2,
        'top_right' => 3,
        'middle_left' => 4,
        'middle_center' => 5,
        'middle_right' => 6,
        'bottom_left' => 7,
        'bottom_center' => 8,
        'bottom_right' => 9
        );
    if (isset($results[$position])) {
        return $results[$position];
    } else {
        return 0;
    }
}
?>
